#include <iostream>
#include <vector>
#include <climits>

using namespace std;

const int INF = 1e9;

int main()
{
    int n, m;
    cin >> n >> m;

    vector<vector<int>> a(n + 1, vector<int>(m + 1, 0));
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= m; ++j)
        {
            cin >> a[i][j];
        }
    }

    vector<vector<int>> v(n + 1, vector<int>(m + 1, INF));
    int t;
    cin >> t;
    for (int i = 0; i < t; ++i)
    {
        int x, y, vv;
        cin >> x >> y >> vv;
        v[x][y] = vv;
    }

    vector<vector<int>> dp(n + 1, vector<int>(m + 1, -INF));
    dp[1][1] = a[1][1];

    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= m; ++j)
        {
            if (i == 1 && j == 1)
                continue;

            int time = i + j - 2;
            if (v[i][j] <= time)
            {
                dp[i][j] = -INF;
            }
            else
            {
                int max_prev = -INF;
                if (i > 1)
                {
                    max_prev = max(max_prev, dp[i - 1][j]);
                }
                if (j > 1)
                {
                    max_prev = max(max_prev, dp[i][j - 1]);
                }
                if (max_prev == -INF)
                {
                    dp[i][j] = -INF;
                }
                else
                {
                    dp[i][j] = max_prev + a[i][j];
                }
            }
        }
    }

    int max_coin = 0;
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= m; ++j)
        {
            if (dp[i][j] > max_coin)
            {
                max_coin = dp[i][j];
            }
        }
    }

    cout << max_coin << endl;

    return 0;
}